import ujson as json
from django.db import models

from base.models import BaseModel
from account.models import UserModel
from account.models import HostModel
from account.models import ScriptModel
from account.models import SettingModel


class WorkModel(BaseModel):
    '''
    工作
    '''
    TYP_RECEIVE = '收货'
    TYP_SEND = '发货'
    TYP_LEVELING = '练级'
    TYP_GATHER = '采集'
    TYP_CLOTH = '买布'
    TYP_CHOICES = (
        (TYP_RECEIVE, '收货'),
        (TYP_SEND, '发货'),
        (TYP_GATHER, '采集'),
        (TYP_LEVELING, '练级'),
        (TYP_CLOTH, '买布'),
    )

    MODE_SOLE = '单人'
    MODE_TEAM = '组队'
    MODE_CHOICES = (
        (MODE_SOLE, '单人'),
        (MODE_TEAM, '组队'),
    )

    host = models.ForeignKey(HostModel, on_delete=models.CASCADE, null=True, default=None)
    name = models.CharField('名称', max_length=128, default='')
    typ = models.CharField('类型', max_length=128, choices=TYP_CHOICES)
    mode = models.CharField('模式', max_length=128, choices=MODE_CHOICES)
    # line为0时，随机生成线路
    line = models.SmallIntegerField('线路', default=0)
    injure_stop = models.BooleanField('受伤停止', default=False)
    soul_stop = models.BooleanField('丢魂停止', default=False)
    auto_restart = models.BooleanField('自动重启', default=True)
    auto_login = models.BooleanField('自动登录', default=True)
    skip_update = models.BooleanField('禁用更新', default=True)
    auto_change_line = models.BooleanField('自动换线', default=False)
    # 针对不同的类型，extra存入不同的数据
    extra = models.TextField('额外信息', null=True, default=None)
    json = models.ForeignKey(SettingModel, on_delete=models.CASCADE, null=True, default=None)
    script = models.ForeignKey(ScriptModel, on_delete=models.CASCADE, null=True, default=None)

    class Meta:
        db_table = 'work'

    def to_dict(self):
        data = super().to_dict()
        data['host'] = self.host.to_dict() if self.host else {}
        data['script'] = self.script.to_dict() if self.script else {}
        data['json'] = self.json.to_dict() if self.json else {}
        return data



class WorkerModel(BaseModel):
    '''
    打工人
    '''
    work = models.ForeignKey(WorkModel, on_delete=models.CASCADE)
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, null=True, default=None)
    json = models.ForeignKey(SettingModel, on_delete=models.CASCADE, null=True, default=None)
    script = models.ForeignKey(ScriptModel, on_delete=models.CASCADE, null=True, default=None)

    class Meta:
        db_table = 'worker'


class DepotModel(BaseModel):
    '''
    仓库
    '''


class LevelingModel(BaseModel):
    '''
    练级
    '''
    pass


class GatherModel(BaseModel):
    '''
    采集
    '''
